package com.lzx.iteam.widget;



import android.content.Context;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.LinearLayout;
import com.lzx.iteam.CloudDialerActivity;
import com.lzx.iteam.R;

/**
 * @author zhaoxuyang
 *
 */
public class HInterceptFrameLayout2 extends LinearLayout{

    int touchSlop;
    PointF start = new PointF();
    Context context;
    CloudDialerActivity activity;
    VelocityTracker vt;
    boolean doubleFinger = false;

    public HInterceptFrameLayout2(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    public void setListener(CloudDialerActivity activity){
        this.activity = activity;
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if(vt==null){
            vt = VelocityTracker.obtain();
        }
        vt.addMovement(ev);
        int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                start.set(ev.getX(), ev.getY());
                break;
            case MotionEvent.ACTION_MOVE:
                float disy = ev.getY()-start.y;
                if(Math.abs(disy)>10){
                    return true;
                }else{
                    if(vt!=null){
                        vt.recycle();
                        vt = null;
                    }
                }
                break;

            default:
                break;
        }
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(vt==null){
            vt = VelocityTracker.obtain();
        }
        vt.addMovement(event);
        int action = event.getAction();
        switch(action){
            case MotionEvent.ACTION_DOWN:
                start.set(event.getX(), event.getY());
                break;
            case MotionEvent.ACTION_MOVE:
                break;

            case MotionEvent.ACTION_UP:
                if(vt!=null){
                    vt.computeCurrentVelocity(1000);
                    float yvt = vt.getYVelocity();
                    if(yvt<0&&!activity.getIsDisplayUnderPanel()){
                        activity.findViewById(R.id.isdisplay_dialpadunder).performClick();
                        return true;
                    }
                    if(yvt>0&&activity.findViewById(R.id.skbcontainer).getVisibility()==View.VISIBLE){
                        activity.findViewById(R.id.btn_displayt9orwords).performClick();
                        return true;
                    }
                }
            /*    if(disy<-getWidth()/2&&first.getVisibility()== View.VISIBLE&&second.getVisibility()==View.GONE){
                    activity.findViewById(R.id.btn_displayt9orwords).performClick();
                    return true;
                }
                if(disy>getWidth()/2&&second.getVisibility()==View.VISIBLE){
                    activity.findViewById(R.id.btn_displayt9orwords).performClick();
                    return true;
                }*/

                vt.recycle();
                vt = null;
                break;
        }
        return true;
    }

}
